<template>
    <div>
        <div class="container">
			<el-table :data="classifyTableData" row-key="id" border  header-cell-class-name="table-header" class="table">
				<el-table-column prop="id" label="ID" width="55" align="center"  v-if="false"></el-table-column>
				<el-table-column prop="classifyName" label="分类名字">
				</el-table-column>
				<el-table-column width="150">
					<template #default="scope">
                    <el-button type="text" icon="el-icon-delete" class="red mr10" @click="func_handlerDel(scope.$index, scope.row)"> 删除</el-button>
                        <el-dropdown :hide-on-click="false">
                            <span class="el-dropdown-link green mr10"><el-icon class="el-icon-lx-info mr10" />更多</span>
                            <template #dropdown>
                                <el-dropdown-menu>
                                    <el-dropdown-item type="text" icon="el-icon-lx-add" @click="func_handleBundDialog(scope.row.id)" v-if="scope.row.classifyGrade<2"> 绑定下级类目</el-dropdown-item>
                                    <el-dropdown-item type="text" icon="el-icon-lx-add" class="green" @click="func_handleBundDialog(scope.row.id)" v-else-if="scope.row.classifyGrade == 2"> 配置属性</el-dropdown-item>
                                </el-dropdown-menu>
                            </template>
                        </el-dropdown>
					</template>
				</el-table-column>
			</el-table>
			<div class="pagination">
				<el-pagination background layout="total, prev, pager, next" :current-page="query.pageNum"
					:page-size="query.pageSize" :total="pageTotal" @current-change="func_handlePageChange"></el-pagination>
			</div>
        </div>
        <el-dialog title="添加产品类目" v-model="editDialogVisible" width="55%" :close-on-click-modal="false">
            <div class="handle-box">
                <VClassify v-bind:selectValue="vClassifyData" @classifydata="func_loadvClassifyData"/>
            </div>
        </el-dialog>
    </div>
</template>
<script>
import { ref, reactive, toRefs, watch } from "vue";
import { useRouter, useRoute } from 'vue-router';
import { ElMessage, ElMessageBox } from "element-plus";
import { findClassifyListChildPage, deleteClassify } from "../../../../api/hairmanager/productClassify";

import { NO_DATA_SELECT_ERROR, SUCCESS_DELDATA_DIALOG_MESSAGE, SUCCESS_EDITDATA_DIALOG_MESSAGE } from "../../../../api/CommonErrorMessage";

import VClassify from "./classifyForm.vue";
export default {
    name: "subClassify",
    components: {VClassify},
  	props:{
		selectValue: Object,
	},
	emits: ["subClassifyList"],
    setup(props, cex) {
        const router = useRouter();
		const route = useRoute();
        let query = reactive({
            parentId: 0,
            pageNum: 1,
            pageSize: 5,
        });
        const classifyTableData = ref([]);
        const pageTotal = ref(0);
        let addFlg = ref(false);
        let tmpParentId = ref(0);
		const editDialogVisible = ref(false);
        // 类目数据参数
        const vClassifyData = reactive({
            id : 0,
            state : false,
        });
// =========================================================================================
        // 获取主表格数据
        const func_getData = () => {
            classifyTableData.value = [];
            findClassifyListChildPage(query).then((res)=>{
                classifyTableData.value = res.data.list;
                pageTotal.value = res.data.total;
            });
            cex.emit('subClassifyList', {state: 1, data: []});
        };
        /**
         * @name 条件查询操作
         */
        const func_handleSearch = () => {
            query.pageNum = 1;
            func_getData();
        };
        /**
         * @name 主表格分页查询
         */
        const func_handlePageChange = (val) => {
            query.pageNum = val;
            func_getData();
        };
        /**
         * @name 类目数据插件引用对话框
         */
        const func_handleBundDialog = (v) => {
            if(v === 0){
                addFlg.value = true;
                vClassifyData['id'] = 0;
                // 初始化插件内表单数据
                vClassifyData['state'] = true;
            } else {
                vClassifyData['id'] = v;
                // 根据ID，读取数据
                vClassifyData['state'] = false;
            }
            editDialogVisible.value = true;
        }
		const func_loadvClassifyData = (v) => {
            if(v['state'] === 1) {
                editDialogVisible.value = false;
            } else {
                editDialogVisible.value = true;
            }
        };
        const func_handlerDel = (index, row) => {
            // 二次确认删除
            ElMessageBox.confirm("确定要删除吗？", "提示", {type: "warning",})
            .then(() => {
                deleteClassify(row['id']).then((res) => {
                    if(res.code != 200) {
                        if(res.type == 0) {
                            ElMessage.warning(res.message);
                        } else {
                            ElMessage.error(res.message);
                        }
                    } else {
                        ElMessage.success(SUCCESS_DELDATA_DIALOG_MESSAGE);
                        classifyTableData.value.splice(index, 1);
                        func_getData();
                    }
                });
            }).catch(() => {});
        }
        // 业务数据监听
        watch(() => props.selectValue, (val) => {
            query.parentId = val['id'];
            val['load'];
            func_getData()
        },{deep: true, immediate: true});
        return {
            // 系统变量 =========================================================================================
            tmpParentId, query, classifyTableData, pageTotal, addFlg, 
            // 主调用方法 =========================================================================================
            func_getData, func_handleSearch, func_handlePageChange, func_handlerDel, func_handleBundDialog,
            // 业务插件处理 =========================================================================================
            editDialogVisible, vClassifyData, func_loadvClassifyData, 
        };
    },
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 180px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.green {
    color: #67c23a;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
.el-dropdown + .el-dropdown {
  margin-left: 15px;
}
.el-dropdown-link {
  cursor: pointer;
  /* color: var(--el-color-primary); */
  color: #67c23a;
  display: flex;
  align-items: center;

}
</style>